package com.googlecode.projects.tests;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 * The purpose of this class 'JDBCCreateDatabase.java' is to test that the
 * applications code can successfully create a new database.
 * 
 * @author Sean
 * 
 */
public class JDBCCreateDatabase {

	/**
	 * Create a main method to run the program.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		/**
		 * Create a new connection and set it to null.
		 */
		Connection connection = null;

		/**
		 * Create a new string to hold the name of the database.
		 */
		String database = "jdbc:derby:/DerbyDB/AssetDB";

		/**
		 * Create a new string to hold the name of the database driver.
		 */
		String databaseDriver = "org.apache.derby.jdbc.EmbeddedDriver";

		/**
		 * Try and create a new database.
		 */
		try {
			Class.forName(databaseDriver).newInstance();
			connection = DriverManager.getConnection(database);
			try {
				Statement statment = connection.createStatement();
				BufferedReader bufferedReader = new BufferedReader(
						new InputStreamReader(System.in));
				System.out.println("What is the database to be called?");
				String databaseName = bufferedReader.readLine();
				statment.executeUpdate("CREATE DATABASE " + databaseName);
				System.out.println("Created the database " + databaseName);
			} catch (Exception e) {
				System.out.println("Unable to execute the statement!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
